package com.sokal.easycare.modules.kpoint.web.activity;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.mapper.JsonMapper;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.kpoint.entity.activity.KPointActivity;
import com.sokal.easycare.modules.kpoint.entity.activity.KPointActivityFile;
import com.sokal.easycare.modules.kpoint.service.activity.KPointActivityFileService;

/**
 * 体验店活动附件Controller
 * @author lihaipei
 * @version 2017-03-19
 */
@Controller
@RequestMapping(value = "${adminPath}/kpoint/activity/kPointActivityFile")
public class KPointActivityFileController extends BaseController {

	@Autowired
	private KPointActivityFileService kPointActivityFileService;
	
	@ModelAttribute
	public KPointActivityFile get(@RequestParam(required=false) String id) {
		KPointActivityFile entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kPointActivityFileService.get(id);
		}
		if (entity == null){
			entity = new KPointActivityFile();
		}
		return entity;
	}
	
	@RequiresPermissions("kpoint:activity:kPointActivityFile:view")
	@RequestMapping(value = {"list", ""})
	public String list(KPointActivityFile kPointActivityFile, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KPointActivityFile> page = kPointActivityFileService.findPage(new Page<KPointActivityFile>(request, response), kPointActivityFile); 
		model.addAttribute("page", page);
		model.addAttribute("kPointActivityFile", kPointActivityFile);
		return "modules/kpoint/activity/kPointActivityFileList";
	}

	@RequiresPermissions("kpoint:activity:kPointActivityFile:view")
	@RequestMapping(value = "form")
	public String form(KPointActivityFile kPointActivityFile, Model model) {
		model.addAttribute("kPointActivityFile", kPointActivityFile);
		return "modules/kpoint/activity/kPointActivityFileForm";
	}

	@RequiresPermissions("kpoint:activity:kPointActivityFile:edit")
	@RequestMapping(value = "save")
	public String save(KPointActivityFile kPointActivityFile, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kPointActivityFile)){
			return form(kPointActivityFile, model);
		}
		kPointActivityFileService.save(kPointActivityFile);
		addMessage(redirectAttributes, "保存附件成功");
		return "redirect:"+Global.getAdminPath()+"/kpoint/activity/kPointActivityFile/?repage";
	}
	
	@RequiresPermissions("kpoint:activity:kPointActivityFile:edit")
	@RequestMapping(value = "delete")
	public String delete(KPointActivityFile kPointActivityFile, RedirectAttributes redirectAttributes) {
		kPointActivityFileService.delete(kPointActivityFile);
		addMessage(redirectAttributes, "删除附件成功");
		return "redirect:"+Global.getAdminPath()+"/kpoint/activity/kPointActivityFile/?repage";
	}

	/**
	 * 跳转至文件管理页
	 * @author lihaipei
	 * @date 2017年4月8日
	 * @param exprStoreId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toFileList")
	public String toFileList(String activityId, Model model) {
		KPointActivity kPointActivity = new KPointActivity(activityId);
		KPointActivityFile kPointActivityFile = new KPointActivityFile();
		kPointActivityFile.setkPointActivity(kPointActivity);
		List<KPointActivityFile> kPointActivityFiles = kPointActivityFileService.findList(kPointActivityFile);
		model.addAttribute("kPointActivityFiles", kPointActivityFiles);
		model.addAttribute("activityId", activityId);
		return "modules/kpoint/activity/fileList";
	}

	/**
	 * 保存体验店活动图片
	 * @author lihaipei
	 * @date 2017年4月8日
	 * @param exprStoreFile 
	 * @param exprStoreId 体验店id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "saveActivityFile")
	public String saveActivityFile(@RequestParam(value = "activityFile") MultipartFile activityFile, String activityId) {
		Map<String, Object> map = Maps.newHashMap();
		try {
			kPointActivityFileService.saveFiles(activityFile, activityId);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	/**
	 * 删除体验店活动图片
	 * @author lihaipei
	 * @date 2017年4月8日
	 * @param exprStoreFileId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "deleteActivityFile")
	public String deleteActivityFile(String activityFileId) {
		Map<String, Object> map = Maps.newHashMap();
		KPointActivityFile kPointActivityFile = new KPointActivityFile(activityFileId);
		kPointActivityFileService.delete(kPointActivityFile);
		map.put("success", true);
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
	/**
	 * 批量修改图片排序号
	 * @author lihaipei
	 * @date 2017年4月8日
	 * @param ids
	 * @param sorts
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts) {
		Map<String, Object> map = Maps.newHashMap();
    	for (int i = 0; i < ids.length; i++) {
    		KPointActivityFile kPointActivityFile = new KPointActivityFile(ids[i]);
    		kPointActivityFile.setSort(sorts[i]);
    		kPointActivityFileService.updateSort(kPointActivityFile);
    	}
		map.put("success", true);
		return JsonMapper.nonDefaultMapper().toJson(map);
	}
	
}